<template>
  <div class="QR-code">
    <div v-for="item in list" :key="item.m_priceid">
      <h3>{{item.priceDesc}}</h3>
      <van-button round type="info" class="btn" v-if="item.qrCode===null" @click="getQR(item)"
        >生成二维码</van-button
      >
      <div class="content" v-else>
        <div>
          <img :src="item.qrCode.qrcode_url" alt="" />
          <p>{{item.priceDesc}}</p>
        </div>
        <div>
          <!-- <van-button round type="info" class="btn"  style="margin-right:30px" @click="getQR(item)">重新生成</van-button> -->
          <van-button round type="warning" class="btn"  @click="downQr(item.qrCode.qrcode_url)">保存二维码</van-button>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { getCodeList, getCode, downloadqrCode } from './service/service.js'
export default {
  name: 'shopAssistant-add',
  data () {
    return {
      userId: '',
      list: []
    }
  },
  created () {
    this.userId = this.$route.query.userId
    this.init()
  },
  computed: {
    loginInfo () {
      return this.$store.state.channel.loginInfo
    }
  },
  methods: {
    init () {
      getCodeList({ shopId: this.userId, type: 1 })
        .then((res) => {
          const data = res.data
          if (data.ret !== 0) {
            this.$toast(data.msg)
            return false
          }
          this.list = data.data
        })
        .catch((err) => {
          console.log(err)
        })
    },
    getQR (val, index) {
      console.log(val)
      getCode({ shopId: this.userId, mpriceid: val.m_priceid })
        .then((res) => {
          const data = res.data
          if (data.ret !== 0) {
            this.$toast(data.msg)
            return false
          }
          // this.$set(this.list[index], 'qrcodeUrl', data.qrcodeUrl)
          this.init()
        })
        .catch((err) => {
          console.log(err)
        })
    },
    downQr (qrcodeUrl) {
      console.log(qrcodeUrl)
      // const eleLink = document.createElement('a')
      // eleLink.download = 'erCode.jpg'
      // eleLink.style.display = 'none'
      // eleLink.href = qrcodeUrl
      // document.body.appendChild(eleLink)
      // eleLink.click()
      // document.body.removeChild(eleLink)
      // this.$saveAs(qrcodeUrl, 'image.jpg')
      // if (this.loginInfo.loginSrc === 0) {
      //   if (navigator.userAgent.indexOf('iPhone') > -1) {
      //     window.location.href = 'ios:savePicture00(' + qrcodeUrl + ')' // 调用ios的函数
      //   } else {
      //     window.injs.savePicture00(qrcodeUrl) // 通过injs接口调用android的函数
      //   }
      // }

      downloadqrCode({ imgUrl: qrcodeUrl })
        .then((res) => {
          // const data = res.data
          // if (data.ret !== 0) {
          //   this.$toast(data.msg)
          //   return false
          // }
          // 创建隐藏的可下载链接
          const link = document.createElement('a')
          const blob = new Blob([res.data], { type: 'image/*' })
          link.style.display = 'none'
          link.href = URL.createObjectURL(blob)
          let num = ''
          for (let i = 0; i < 10; i++) {
            num += Math.ceil(Math.random() * 10)
          }
          link.setAttribute('download', '二维码_' + num + '.png')
          document.body.appendChild(link)
          link.click()
          document.body.removeChild(link)

          // this.$saveAs(res.data, 'erCode.png')
        })
        .catch((err) => {
          console.log(err)
        })
    }
  }
}
</script>
<style lang="less" scoped>
.QR-code {
  padding: 15px;
  box-sizing: border-box;
  > div {
    text-align: center;
    background: #fff;
    border-radius: 8px;
    padding: 15px 0 30px;
    margin-bottom: 15px;
    h3 {
      font-size: 20px;

      font-weight: 700;
      color: #333333;
      line-height: 28px;
    }
    .btn {
      padding: 0 30px;
      margin-top: 40px;
    }
  }
  .content {
    > div:first-child {
      width: 220px;
      border: 1px solid #dddddd;
      padding: 10px;
      text-align: center;
      display: inline-block;
      margin-top: 20px;
      > img {
        width: 100%;
      }
      > p {
        margin-top: 10px;
        color: #999;
      }
    }

  }
}
</style>
